package com.gopay.common.util.xml.utils.converters;


public class NumberConverter extends AbstractConverter{
	
	public NumberConverter(Object defaultValue){
		this(true, defaultValue);
	}
	
	public NumberConverter(boolean isUseDefaultValue, Object defaultValue){
		this.isUseDefaultValue = isUseDefaultValue;
		setDefaultValue(defaultValue);
	}

	@Override
    public Object convert(Object value)throws Exception{
		if(value == null){
			value = isUseDefaultValue ? getDefaultValue() : value;
			return value;
		}
		if(value instanceof String){
			if(value.toString().trim().equals("")){
				value = isUseDefaultValue ? getDefaultValue() : value;
				return value;
			}
			Integer i = Integer.valueOf((String)value);
			return i;
		}
		throw new IllegalArgumentException("the param \"value\" is not String: " + value);
	}
}
